<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/27
 * Time: 16:46
 */

namespace app\wap\controller;


use think\Config;
use think\Db;
use think\Exception;

class Realname extends Basewap
{

    //渠道获取
    public function channel(){
        $data=  db('transferchannel')->where('state',0)->field('channel_id asid,name')->select();
        if ($data==true){
            return json_encode(['msg'=>'ok','data'=>$data,'code'=>0]);
        }else{
            return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        }
    }


    // 实名提交
    public function real(){
        $post= request()->post();
        if (empty($post['member_id'])) return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        $exist = db('member_real')->where('member_id',$post['member_id'])->find();
        if(empty($exist)){
            $data = db('member_real')->insert([
                'member_id'=>$post['member_id'],
                'name'=>$post['name'],
                'wechat'=>!empty($post['wechat'])?$post['wechat']:null,
                'phone'=>$post['phone'],
                'channel_id'=>$post['channel_id'],
                'account'=>$post['account'],
                'message'=>!empty($post['message'])?$post['message']:null,
            ]);
        }else{
            $data = db('member_real')->where('member_id',$post['member_id'])->update([
                'name'=>$post['name'],
                'wechat'=>!empty($post['wechat'])?$post['wechat']:null,
                'phone'=>$post['phone'],
                'channel_id'=>$post['channel_id'],
                'account'=>$post['account'],
                'message'=>!empty($post['message'])?$post['message']:null,
            ]);
        }

        if ($data!==false){
            return json_encode(['msg'=>'ok','data'=>"",'code'=>0]);
        }else{
            return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        }
    }
    //实名信息修改
    public function realedit(){
        $post= request()->post();
        if (empty($post)) return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        if (empty($post['name'])){
            $data=  db('member_real') ->where('member_id',$post['member_id'])->find();
            return json_encode(['msg'=>'ok','data'=>$data,'code'=>0]);
        }else{

            $data=   db('member_real')->where('real_id',$post['real_id'])->update([
                'member_id'=>$post['member_id'],
                'name'=>$post['name'],
                'wechat'=>!empty($post['wechat'])?$post['wechat']:null,
                'phone'=>$post['phone'],
                'channel_id'=>$post['channel_id'],
                'account'=>$post['account'],
                'message'=>!empty($post['message'])?$post['message']:null,
            ]);
            if ($data==true){
                return json_encode(['msg'=>'ok','data'=>"",'code'=>0]);
            }else{
                return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
            }
        }
    }


    //我的收益
    public function profit(){
        $member_id=  input('member_id');
        $member = db('member')->find($member_id);
        if(empty($member)) return json_encode(['msg'=>'用户身份有误','code'=>101]);

        $member_real= db('member_real')->alias('a')
            ->where('a.member_id',$member_id)
            ->join('transferchannel b','b.channel_id=a.channel_id')
            ->field('a.*,b.name as channel_name ')
            ->find();
        //预计收益 未入账的佣金
        $distributiondetails = db('distributiondetails')
            ->field('sum(money) as money')
            ->where(['member_id'=>$member_id,'enter_account'=>0])
            ->find();

        $data=[
            'expect_money'=>$distributiondetails['money']??0,
            'member_money'=>$member['member_money'],
            'member_real'=>!empty($member_real)?$member_real:0
        ];
        return json_encode(['msg'=>'ok','code'=>0,'data'=>$data]);
    }


    //提现申请
    public function rutforward(){
        $post=request()->post();
        if (empty($post['money']))return json_encode(['msg'=>'参数错误 为空','data'=>'','code'=>102]);
        $d= db('putforward')->where([ 'member_id'=>$post['member_id']])->find();
        if (($d['time']+300)>time()){
            return json_encode(['msg'=>'300秒内只能发起一次提现','data'=>'','code'=>104]);
        }

        Db::startTrans();
        try{
            $datas= db('member')->where('member_id',$post['member_id'])->setDec('member_money', (float)$post['money']);
            $data= db('putforward')->insert([
                'member_id'=>$post['member_id'],
                'money'=>$post['money'],
                'type'=>0,
                'state'=>1,
                'time'=>time()
            ]);
            // 提交事务
            Db::commit();
            return json_encode(['msg'=>'ok','data'=>'','code'=>0]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json_encode(['msg'=>'参数错误 写入数据失败','error'=>$e->getMessage(),'code'=>105]);
        }
    }


    //累计收益
    public function income(){
        $member_id= input('member_id');
        if (empty($member_id))  return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        $data= db('distributiondetails')
            ->where(['member_id'=>$member_id,'enter_account'=>1])
            ->order('id desc')
            ->select();

        foreach ($data as $k=>$v) {
            if($v['consumption_goods']>0){
                $goods= db('goods')->where('goods_id',$v['consumption_goods'])->field("goods_name")->find();
                $data[$k]['goods_name'] = $goods['goods_name'];
            }else{
                $data[$k]['goods_name'] = $v['order_name'];
            }
        }

        $dat=[];
        if ($data){
            foreach ($data as $v){
                if($v['consumption_member_id']){
                    $g=  db('member')
                        ->alias('a')
                        ->where('a.member_id',$v['consumption_member_id'])
                        ->join('attachment b','b.id=a.thumb')
                        ->field('a.member_nickname,b.filepath as portrait')
                        ->find();
                    $v['consumption_member_name']=$g['member_nickname'];
                    $v['portrait'] = !empty($g['portrait'])?$g['portrait']:Config::get('default_member_headimg');
                    $v['consumption_time']=date('Y-m-d H:i:s',$v['consumption_time']);
                }elseif($v['consumption_member_username']){
                    $g=  db('member')
                        ->alias('a')
                        ->where('a.member_username',$v['consumption_member_username'])
                        ->join('attachment b','b.id=a.thumb')
                        ->field('a.member_nickname,b.filepath as portrait')
                        ->find();
                    $v['consumption_member_name']=$g['member_nickname'];
                    $v['portrait'] = !empty($g['portrait'])?$g['portrait']:Config::get('default_member_headimg');
                    $v['consumption_time']=date('Y-m-d H:i:s',$v['consumption_time']);
                }
                $v['state_title'] = $v['enter_account']>0?"已入账":"待入账";
                $dat[]=$v;
            }
            return json_encode(['msg'=>'ok','data'=>$dat,'code'=>0]);
        }else{
            return json_encode(['msg'=>'没有更多数据','data'=>'','code'=>101]);
        }
    }
    //预计收益列表
    public function expectIncome(){
        $member_id= input('member_id');
        if (empty($member_id))  return json_encode(['msg'=>'参数错误','data'=>'','code'=>101]);
        $data= db('distributiondetails')
            ->where(['member_id'=>$member_id,'enter_account'=>0])
            ->order('id desc')
            ->select();

        foreach ($data as $k=>$v) {
            if($v['consumption_goods']>0){
                $goods= db('goods')->where('goods_id',$v['consumption_goods'])->field("goods_name")->find();
                $data[$k]['goods_name'] = $goods['goods_name'];
            }else{
                $data[$k]['goods_name'] = $v['order_name'];
            }
        }

        $dat=[];
        if ($data){
            foreach ($data as $v){
                if($v['consumption_member_id']){
                    $g=  db('member')
                        ->alias('a')
                        ->where('a.member_id',$v['consumption_member_id'])
                        ->join('attachment b','b.id=a.thumb')
                        ->field('a.member_nickname,b.filepath as portrait')
                        ->find();
                    $v['consumption_member_name']=$g['member_nickname'];
                    $v['portrait'] = !empty($g['portrait'])?$g['portrait']:Config::get('default_member_headimg');
                    $v['consumption_time']=date('Y-m-d H:i:s',$v['consumption_time']);
                }elseif($v['consumption_member_username']){
                    $g=  db('member')
                        ->alias('a')
                        ->where('a.member_username',$v['consumption_member_username'])
                        ->join('attachment b','b.id=a.thumb')
                        ->field('a.member_nickname,b.filepath as portrait')
                        ->find();
                    $v['consumption_member_name']=$g['member_nickname'];
                    $v['portrait'] = !empty($g['portrait'])?$g['portrait']:Config::get('default_member_headimg');
                    $v['consumption_time']=date('Y-m-d H:i:s',$v['consumption_time']);
                }
                $v['state_title'] = $v['enter_account']>0?"已入账":"待入账";
                $dat[]=$v;
            }
            return json_encode(['msg'=>'ok','data'=>$dat,'code'=>0]);
        }else{
            return json_encode(['msg'=>'没有更多数据','data'=>'','code'=>101]);
        }
    }

    public function record(){
        $member_id=  input('member_id');
        $data=db('putforward')->where('member_id',$member_id)->field('id,money,time,state')->order('time desc')->select();
        $dat=[];
        if ($data){
            foreach ($data as $v){
                $v['time']=date('Y-m-d H:i:s',$v['time']);
                $dat[]=$v;
            }
            return json_encode(['msg'=>'ok','data'=>$dat,'code'=>0]);
        }else{
            return json_encode(['msg'=>'没有更多数据','data'=>'','code'=>102]);
        }
    }



}